package com.zjp.blog.service;

import com.zjp.blog.data.client.ConfigMapper;
import com.zjp.blog.data.model.Config;
import com.zjp.blog.utils.Validation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author pj_zhong
 * @email pj_zhong@163.com / pj_zhong@gmail.com
 */

@Service
public class ConfigService {

    @Autowired
    ConfigMapper configMapper;

    public Config getById(int id){
        return configMapper.selectByPrimaryKey(id);
    }

    public Config getByKey(String key){
        return configMapper.selectByKey(key);
    }

    public int updateById(Config config){
        return configMapper.updateByPrimaryKey(config);
    }

    public int updateByKey(Config config){
        if(Validation.isEmpty(config.getKey())){
            return 0;
        }

        return configMapper.updateByKey(config);
    }

    public int deleteById(int id){
        return configMapper.deleteByPrimaryKey(id);
    }

    public int deleteByKey(String key){
        if(Validation.isEmpty(key)){
            return 0;
        }

        return configMapper.deleteByKey(key);
    }

    /**
     * 插入系统配置 ，如果 key or value 为空值则返回null,否则返回 config;
     * @param config
     * @return
     */
    public Config insertConfig(Config config){
        if(Validation.isEmpty(config.getKey()) || Validation.isEmpty(config.getValue())){
            return null;
        }

        configMapper.insert(config);

        return config;
    }


    public List<Config> listAll(){
        return configMapper.listAll();
    }


}
